﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VipSys.SystemEntity;
using VipSys.Common.Library;
using VipSys.DataLayer;
using System.Collections;

namespace VipSys.Business
{
    public class HotelRegReport
    {
        private static HotelRegReport m_Instance = null;
        /// <summary>
        /// 单实例
        /// </summary>
        public static HotelRegReport Instance
        {
            get
            {
                if (m_Instance == null)
                    m_Instance = new HotelRegReport();
                return m_Instance;
            }
        }

        /// <summary>
        /// 導出試題
        /// </summary>
        /// <param name="filePath">導出的位置</param>
        /// <param name="subjects">導出的題目</param>
        public short Export(string filePath, List<User> lstUser)
        {
            ExcelOpr excelOpr = new ExcelOpr();
            string[] columnName = new string[]{
                "业务员","报名次序","姓名","性别","所在学校","所报学校","网报序号","网报密码","联系方式","备注","所交款额","提交考点","房号","酒店","酒店房价","多退少补",""
            };
            List<string[]> values = new List<string[]>();
            for (int i = 0; i < lstUser.Count; i++)
            {
                User user = lstUser[i];
                string[] value = new string[17]{
                      user.EmployeeName
                    , user.Sequence
                    , user.Name
                    , user.Gender
                    , user.School
                    , user.ExamSchool
                    , user.ID.ToString()
                    , user.PassWord
                    , user.Tel
                    , user.Bak
                    , user.PayMoney
                    , user.ExamPlace
                    , user.Room
                    , user.Hotel
                    , user.HotelExpense
                    , user.MoneyBack
                    ,""
                };
                values.Add(value);

            }
            try
            {
                excelOpr.ToExcel(filePath, columnName, values);
            }
            catch (Exception)
            {
                return SystemData.ReturnValue.OTHER_ERROR;
            }
            return SystemData.ReturnValue.OK;
        }
        /// <summary>
        /// 導出試題
        /// </summary>
        /// <param name="filePath">導出的位置</param>
        /// <param name="subjects">導出的題目</param>
        public short Export(string filePath, List<User> lstUser, string templateValue)
        {
            ExcelOpr excelOpr = new ExcelOpr();
            string[] columnName = templateValue.Split(',');
            List<ArrayList> values = new List<ArrayList>();
            for (int i = 0; i < lstUser.Count; i++)
            {
                User user = lstUser[i];
                ArrayList value = new ArrayList();
                for (int j = 0; j < columnName.Length; j++)
                {
                    value.Add(GetColumnValue(user, columnName[j]));
                }
                value.Add("");
                values.Add(value);
            }
            try
            {
                excelOpr.ToExcel(filePath, columnName, values);
            }
            catch (Exception)
            {
                return SystemData.ReturnValue.OTHER_ERROR;
            }
            return SystemData.ReturnValue.OK;
        }

        private static string GetColumnValue(User user, string columnName)
        {
            switch (columnName)
            {
                case "备注":
                    return user.Bak;
                case "业务员":
                    return user.EmployeeName;
                case "网报序号":
                    return user.ID.ToString();
                case "报名次序":
                    return user.Sequence;
                case "姓名":
                    return user.Name;
                case "性别":
                    return user.Gender;
                case "所在学校":
                    return user.School;
                case "报考类型":
                    return user.Template;
                case "联系方式":
                    return user.Tel;
                case "意向同住人":
                    return user.ExceptRoomie;
                case "提交考点":
                    return user.ExamPlace;
                case "已交款额":
                    return user.PayMoney;
                case "房号":
                    return user.Room;
                case "酒店":
                    return user.Hotel;
                case "酒店房价":
                    return user.HotelExpense;
                case "多退少补":
                    return user.MoneyBack;
                case "所报学校":
                    return user.ExamSchool;
                case "网报密码":
                    return user.PassWord;
                case "收缴余款所在地":
                    return user.PayPlace;
                default:
                    break;
            }
            return string.Empty;
        }
        /// <summary>
        /// 導入試題
        /// </summary>
        /// <param name="filePath">試題文件路徑</param>
        /// <returns>是否導入成功</returns>
        public short Import(string filePath)
        {
            ExcelProvider excelProvider = ExcelProvider.Create(filePath, "Sheet1");
            foreach (ExcelRow row in excelProvider)
            {
                int ID = 0;
                if (!string.IsNullOrEmpty(row.GetString("网报序号")))
                    ID = int.Parse(row.GetString("网报序号"));
                string MoneyBack = row.GetString("多退少补");
                string HotelExpense = row.GetString("酒店房价");
                string Hotel = row.GetString("酒店");
                string Room = row.GetString("房号");
                string PayMoney = row.GetString("所交款额");
                string Bak = row.GetString("备注");
                string Tel = row.GetString("联系方式");
                string PassWord = row.GetString("网报密码");
                string School = row.GetString("所在学校");
                string Gender = row.GetString("性别");
                string Name = row.GetString("姓名");
                string Sequence = row.GetString("报名次序");
                string EmployeeName = row.GetString("业务员");
                string ExamSchool = row.GetString("所报学校");
                User user = new User()
                {
                    MoneyBack = row.GetString("多退少补")
                    ,
                    HotelExpense = row.GetString("酒店房价")
                    ,
                    ID = string.IsNullOrEmpty(row.GetString("网报序号")) ? 0 : int.Parse(row.GetString("网报序号"))
                    ,
                    Hotel = row.GetString("酒店")
                    ,
                    Room = row.GetString("房号")
                    ,
                    ExamPlace = row.GetString("提交考点")
                    ,
                    PayMoney = row.GetString("所交款额")
                    ,
                    Bak = row.GetString("备注")
                    ,
                    Tel = row.GetString("联系方式")
                    ,
                    PassWord = row.GetString("网报密码")
                    ,
                    ExamSchool = row.GetString("所报学校")
                    ,
                    School = row.GetString("所在学校")
                    ,
                    Gender = row.GetString("性别")
                    ,
                    Name = row.GetString("姓名")
                    ,
                    Sequence = row.GetString("报名次序")
                    ,
                    EmployeeName = row.GetString("业务员")
                };
                Employee query = new Employee();
                query.Name = user.EmployeeName;
                short shRet = EmployeeAccess.Instance.GetEmployee(query, ref query);
                user.EmployeeID = query.ID.ToString();
                if (user.ID != 0)
                    shRet = UserAccess.Instance.Edit(user);
                else
                {
                    user.PassWord = AccountCreat.Instance.GetPwd(Tel);
                    shRet = UserAccess.Instance.Add(user);
                }
                if (shRet != SystemData.ReturnValue.OK)
                    return shRet;
            }
            return SystemData.ReturnValue.OK;
        }
        /// <summary>
        /// 導入試題
        /// </summary>
        /// <param name="filePath">試題文件路徑</param>
        /// <returns>是否導入成功</returns>
        public short Import(string filePath, string szTemplateValue)
        {
            if (string.IsNullOrEmpty(szTemplateValue))
                return SystemData.ReturnValue.PARAM_ERROR;
            string[] values = szTemplateValue.Split(',');
            ExcelProvider excelProvider = ExcelProvider.Create(filePath, "Sheet1");
            foreach (ExcelRow row in excelProvider)
            {

                int ID = 0;
                User user = new User();
                for (int index = 0; index < values.Length; index++)
                {
                    switch (values[index])
                    {
                        case "网报序号":
                            if (!string.IsNullOrEmpty(row.GetString("网报序号")))
                            user.ID = int.Parse(row.GetString(values[index]));
                            break;
                        case "多退少补":
                            user.MoneyBack = row.GetString(values[index]);
                            break;
                        case "酒店房价":
                            user.HotelExpense = row.GetString(values[index]);
                            break;
                        case "酒店":
                            user.Hotel = row.GetString(values[index]);
                            break;
                        case "房号":
                            user.Room = row.GetString(values[index]);
                            break;
                        case "已交款额":
                            user.PayMoney = row.GetString(values[index]);
                            break;
                        case "备注":
                            user.Bak = row.GetString(values[index]);
                            break;
                        case "联系方式":
                            user.Tel = row.GetString(values[index]);
                            break;
                        case "网报密码":
                            user.PassWord = row.GetString(values[index]);
                            break;
                        case "所在学校":
                            user.School = row.GetString(values[index]);
                            break;
                        case "性别":
                            user.Gender = row.GetString(values[index]);
                            break;
                        case "报名次序":
                            user.Sequence = row.GetString(values[index]);
                            break;
                        case "业务员":
                            user.EmployeeName = row.GetString(values[index]);
                            break;
                        case "意向同住人":
                            user.ExceptRoomie = row.GetString(values[index]);
                            break;
                        case "考试类型":
                            user.Template = row.GetString(values[index]);
                            break;
                        case "收缴余款所在地":
                            user.PayPlace = row.GetString(values[index]);
                            break;
                        case "所报学校":
                            user.ExamSchool = row.GetString(values[index]);
                            break;
                        case "姓名":
                            user.Name = row.GetString(values[index]);
                            break;
                        case "提交考点":
                            user.ExamPlace = row.GetString(values[index]);
                            break;
                        default:
                            break;
                    }
                }
                Employee query = new Employee();
                short shRet = EmployeeAccess.Instance.GetEmployeeByName(user.EmployeeName, ref query);
                user.EmployeeID = query.ID.ToString();
                //判断考生是否已导入
                User queryUser=new User();
                shRet = UserAccess.Instance.GetUserByTel(user.Tel, ref queryUser);
                if (shRet == SystemData.ReturnValue.OK)
                {
                    //重复导入时密码重置为111111，excel有网报序号时密码无需重置
                    if (user.ID.ToString() == ""||user.ID==0)
                    {
                        user.PassWord = AccountCreat.Instance.GetPwd(user.Tel);
                    }
                    shRet = UserAccess.Instance.EditByTel(user);
                }
                else
                {
                    user.PassWord = AccountCreat.Instance.GetPwd(user.Tel);
                    shRet = UserAccess.Instance.Add(user);
                }
                if (shRet != SystemData.ReturnValue.OK)
                    return shRet;
            }
            return SystemData.ReturnValue.OK;
        }
    }
}
